package com.woniuxy.redis.lock.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.redis.lock.entity.Stu;
import com.woniuxy.redis.lock.mapper.StuMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * description: MyConsumer <br>
 * date: 2022/1/13 10:22 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
//消费者

@Component
public class MyConsumer {

    @Autowired
    StuMapper stuMapper;

    @RabbitListener(queues = "queueadd")
    public void receiveQueueadd(Stu stu, Message message, Channel channel) throws IOException {
        System.out.println("stu:" + stu);
        //添加到数据库
        stuMapper.insertStu(stu);
        //消费这个队列，每次从队列中取一个对象，手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
